package com.iwhalecloud.bss.kite.cucc.order.save;

import com.iwhalecloud.bss.kite.cucc.order.save.impl.BaseSaveHandler;
import com.iwhalecloud.bss.kite.cucc.order.save.impl.InstSaveHandler;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.IInst;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 实例&订单入库处理器工厂
 */
public abstract class SaveHandlerFactory {

    /**
     * 实体类和入库处理器映射
     */
    private static final Map<Class<?>, Class<? extends ISaveHandler<?>>> SAVE_HANDLER_MAP = new HashMap<>();

    /**
     * 根据实体类获取入库处理器
     * @param entityClass
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <E, T extends ISaveHandler<E>> T getSaveHandler(Class<?> entityClass) {
        if (Objects.isNull(entityClass)) {
            return (T) SpringUtil.getBean(StringUtils.uncapitalize(BaseSaveHandler.class.getSimpleName()));
        }

        Class<? extends ISaveHandler<?>> handlerClass = SAVE_HANDLER_MAP.get(entityClass);
        if (Objects.nonNull(handlerClass)) {
            return (T) SpringUtil.getBean(handlerClass);
        }
        else if (IInst.class.isAssignableFrom(entityClass)) {
            return (T) SpringUtil.getBean(InstSaveHandler.class);
        }
        else {
            return (T) SpringUtil.getBean(StringUtils.uncapitalize(BaseSaveHandler.class.getSimpleName()));
        }
    }
}
